-- Profiler
-- Create by zouyb
-- 性能统计

require("core/mobile/Platform");

if isWindowsPlatform() then
    require "ShinyLua"
end

module("Profiler", package.seeall);

-- 统计信息
local profileInfo = {};
local socket = require "socket";

-- 函数开始
function funcBegin(funcName)
    if DEBUG_MODE ~= 1 then return end

    --获取毫秒级时间
	if profileInfo[funcName] == nil then
        profileInfo[funcName] = { count = 0, total = 0, max = 0 };
	end

    profileInfo[funcName].beginTime = socket.gettime();
end

-- 函数结束
function funcEnd(funcName)
    if DEBUG_MODE ~= 1 then return end

	local info = profileInfo[funcName];

    if not info then return end

    local time = socket.gettime() - info.beginTime;
	info.count = info.count + 1;
    info.total = info.total + time;
    info.max = math.max(info.max, time);
end

-- 排序函数
local sortFunc = function(f1, f2)
    return f1.avg > f2.avg;
end

-- 获取打印字符串
function printResult()
    local t = {};
	for funcName, info in pairs(profileInfo) do
        table.insert(t, {
            name = funcName,
            count = info.count,
            total = info.total,
            avg = info.total / info.count,
            max = info.max,
        });
	end
    table.sort(t, sortFunc);

	for _, info in pairs(t) do
        print(string.format("[%s] count:%d, avg:%.05f(max=%.04f)", info.name, info.count,
            info.avg, info.max));
	end
end

-- 重置
function reset()
    profileInfo = {};
end

-- 统计socket读写相关
local socketReadCost, socketUnpackCost = {}, {};
function addSocketRead(cmd, time)
    if DEBUG_MODE ~= 1 then return end
    if nil == socketReadCost[cmd] then
        socketReadCost[cmd] = { total = time, count = 1, max = time };
    else
        socketReadCost[cmd]["count"] = socketReadCost[cmd]["count"] + 1;
        socketReadCost[cmd]["total"] = socketReadCost[cmd]["total"] + time;
        socketReadCost[cmd]["max"] = math.max(socketReadCost[cmd]["max"], time);
    end
end
function addUnpack(cmd, time)
    if DEBUG_MODE ~= 1 then return end
    if nil == socketUnpackCost[cmd] then
        socketUnpackCost[cmd] = { total = time, count = 1, max = time };
    else
        socketUnpackCost[cmd]["count"] = socketUnpackCost[cmd]["count"] + 1;
        socketUnpackCost[cmd]["total"] = socketUnpackCost[cmd]["total"] + time;
        socketUnpackCost[cmd]["max"] = math.max(socketUnpackCost[cmd]["max"], time);
    end
end
function printSocket()
    local t = {};
	for cmd, info in pairs(socketReadCost) do
        table.insert(t, {
            cmd = MSG.getMsgName(cmd),
            count = info.count,
            total = info.total,
            avg = info.total / info.count,
            max = info.max
        });
	end
    table.sort(t, sortFunc);

    print("\n=========== read socket =========");
	for _, temp in pairs(t) do
        if temp.cmd then
            print(string.format("[%s] count:%d, avg:%.05f(max=%.04f)", temp.cmd, temp.count,
                temp.total/temp.count, temp.max));
        end
	end

    t = {};
    for cmd, info in pairs(socketUnpackCost) do
        table.insert(t, {
            cmd = MSG.getMsgName(cmd),
            count = info.count,
            total = info.total,
            avg = info.total / info.count,
            max = info.max
        });
	end

    table.sort(t, sortFunc);
    print("\n=========== unpack socket =========");
	for _, temp in pairs(t) do
        if temp.cmd then
            print(string.format("[%s] count:%d, avg:%.05f(max=%.04f)", temp.cmd, temp.count,
                temp.total/temp.count, temp.max));
        end
	end
end
